package com.fhd.service.Recharge;

import com.fhd.dao.BaseDao;
import com.fhd.dao.Client.ClientDao;
import com.fhd.dao.Client.ClientDaoImplement;
import com.fhd.dao.Recharge.RechargeDao;
import com.fhd.dao.Recharge.RechargeDaoImplement;
import com.fhd.pojo.Client;
import com.fhd.pojo.Recharge;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Objects;

public class RechargeServiceImplement implements RechargeService {

    //调用Dao层
    private RechargeDao rechargeDao = null;
    public RechargeServiceImplement() {
        rechargeDao = new RechargeDaoImplement();
    }

    // 获取某个特定的充值对象
    @Override
    public Recharge getOneRecharge(int id) {
        Connection conn = null;
        Recharge recharge = null;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            recharge = rechargeDao.getOneRecharge(conn, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return recharge;
    }

    // 获取某个用户所有的充值数据
    @Override
    public ArrayList<Recharge> getSomeoneRecharge(int clientId) {
        Connection conn = null;
        ArrayList<Recharge> recharges = null;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            recharges = rechargeDao.getSomeoneRecharge(conn, clientId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return recharges;
    }

    // 获取所有的充值数据
    @Override
    public ArrayList<Recharge> getAllRecharge() {
        Connection conn = null;
        ArrayList<Recharge> recharges = null;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            recharges = rechargeDao.getAllRecharge(conn);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return recharges;
    }

    // 创建充值数据
    @Override
    public int clientRecharge(int money, int clientId) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            affectedRow = rechargeDao.clientRecharge(conn,clientId,money);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }

    // 改变充值状态
    @Override
    public int managerChangeRechargeSituation(int id, int situation) {
        Connection conn = null;
        int affectedRow = 0;
        try {
            // 获取连接
            conn = BaseDao.getConnection();
            affectedRow = rechargeDao.managerChangeRechargeSituation(conn,id,situation);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            BaseDao.close(conn,null,null);
        }
        return affectedRow;
    }
}
